package com.shf.dynamodb;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.shf.dynamodb.util.DynamoDBClientUtil;

/**
 * 异常错误处理
 * @author songhaifeng
 *
 */
public class ErrorHandleDemo {

	public static void main(String[] args) {
		AmazonDynamoDBClient amazonDynamoDBClient=DynamoDBClientUtil.getDynamoclientWithRetry();
		DynamoDB dynamoDB = new DynamoDB(amazonDynamoDBClient);
		Table table = dynamoDB.getTable("Movies");
		
		try {
		    Item item = table.getItem("year", 1978, "title", "Superman");
		    if (item != null) {
		        System.out.println("Result: " + item);
		    } else { 
		        //No such item exists in the table
		        System.out.println("Item not found");
		    }

		} catch (AmazonServiceException ase) {// 客户端请求正确地传输到 DynamoDB 但 DynamoDB 无法处理请求并返回错误响应时引发。
		    System.err.println("Could not complete operation");
		    System.err.println("Error Message:  " + ase.getMessage());
		    System.err.println("HTTP Status:    " + ase.getStatusCode());
		    System.err.println("AWS Error Code: " + ase.getErrorCode());
		    System.err.println("Error Type:     " + ase.getErrorType());
		    System.err.println("Request ID:     " + ase.getRequestId());

		} catch (AmazonClientException ace) {//客户端无法从服务获得响应，或者客户端无法解析来自服务的响应时引发。
		    System.err.println("Internal error occured communicating with DynamoDB");
		    System.out.println("Error Message:  " + ace.getMessage());
		}
	}
	
}
